package com.javasm.mingming.login.authorize;

import com.javasm.mingming.login.entity.LoginUserDetails;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @author javasm
 * @version 0.1
 * @className JavasmExpressAuthorize
 * @descriptioin:
 * @date 2025/1/15 14:36
 * @since jdk11
 */
@Component
public class JavasmExpressAuthorize {
    public boolean f1(String code){
        //code 用户的权限标识,如何判断 用户是否拥有这个权限标识
        //获取当前登录的用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal!=null){
            LoginUserDetails loginUserDetails = (LoginUserDetails) principal;
            //获取 权限列表
            Collection<? extends GrantedAuthority> authorities =
                    loginUserDetails.getAuthorities();
            //判断 里面 是否包含字符串 code
            return authorities.stream().anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals(code));
        }
        return false;
    }
}
